import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '../store/auth'
import { useMenuStore } from '../store/menu'

const routes: RouteRecordRaw[] = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: () => import('../pages/Home.vue') },
  { path: '/login', component: () => import('../pages/Login.vue') },
  { path: '/profile', component: () => import('../pages/Profile.vue'), meta: { requiresAuth: true } },
  { path: '/admin', redirect: '/admin/dashboard' },
  { path: '/admin/dashboard', component: () => import('../pages/admin/Dashboard.vue'), meta: { requiresAuth: true } },
  { path: '/admin/activities', component: () => import('../pages/admin/Activities.vue'), meta: { requiresAuth: true } },
  { path: '/admin/events', component: () => import('../pages/admin/Events.vue'), meta: { requiresAuth: true } },
  { path: '/admin/evaluations', component: () => import('../pages/admin/Evaluations.vue'), meta: { requiresAuth: true } },
  { path: '/admin/reports', component: () => import('../pages/admin/Reports.vue'), meta: { requiresAuth: true } },
  { path: '/admin/report-headers', component: () => import('../pages/admin/ReportHeaders.vue'), meta: { requiresAuth: true } },
  { path: '/admin/report-headers/:headerId/versions', component: () => import('../pages/admin/ReportVersions.vue'), meta: { requiresAuth: true } },
  { path: '/admin/report-versions/:versionId/editor', component: () => import('../pages/admin/ReportEditor.vue'), meta: { requiresAuth: true } },
  { path: '/admin/evaluation-templates', component: () => import('../pages/admin/EvaluationTemplates.vue'), meta: { requiresAuth: true } },
  { path: '/admin/evaluation-templates/:id', component: () => import('../pages/admin/TemplateDetail.vue'), meta: { requiresAuth: true } },
  { path: '/admin/llm-config', component: () => import('../pages/admin/LLMConfig.vue'), meta: { requiresAuth: true } },
  { path: '/admin/users', component: () => import('../pages/admin/Users.vue'), meta: { requiresAuth: true } },
  { path: '/admin/roles', component: () => import('../pages/admin/Roles.vue'), meta: { requiresAuth: true } },
  { path: '/admin/menus', component: () => import('../pages/admin/Menus.vue'), meta: { requiresAuth: true } },
  { path: '/admin/dictionaries', component: () => import('../pages/admin/Dictionaries.vue'), meta: { requiresAuth: true } },
  { path: '/admin/indicators', component: () => import('../pages/admin/Indicators.vue'), meta: { requiresAuth: true } },
  { path: '/admin/activity-scoring', component: () => import('../pages/admin/ActivityScoring.vue'), meta: { requiresAuth: true } },
  { path: '/admin/activity-scoring/:activityId', component: () => import('../pages/admin/ActivityScoringDetail.vue'), meta: { requiresAuth: true } },
  { path: '/admin/archives', component: () => import('../pages/admin/ActivityArchives.vue'), meta: { requiresAuth: true } },
  { path: '/admin/archives/:activityId', component: () => import('../pages/admin/ActivityArchives.vue'), meta: { requiresAuth: true } },
  { path: '/admin/topics', component: () => import('../pages/admin/Topics.vue'), meta: { requiresAuth: true } },
  { path: '/admin/crawling-tasks', component: () => import('../pages/admin/CrawlingTasks.vue'), meta: { requiresAuth: true } },
  { path: '/admin/social-contents', component: () => import('../pages/admin/SocialContent.vue'), meta: { requiresAuth: true } },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to, _from, next) => {
  const auth = useAuthStore()
  const menu = useMenuStore()
  if (to.path === '/login') return next()
  if (to.path === '/home') return next()
  if (to.meta?.requiresAuth && !auth.token) return next('/login')
  // 基于角色菜单权限限制访问（当菜单已加载时）
  if (to.meta?.requiresAuth && auth.token && menu.allowedPaths?.length) {
    const allowed = new Set(menu.allowedPaths)
    const white = new Set(['/profile'])
    const path = to.path
    const isAllowed = Array.from(allowed).some((p) => path === p || path.startsWith(p + '/'))
    if (!isAllowed && !white.has(path)) {
      const target = menu.firstAdminPath || '/home'
      return next(target)
    }
  }
  next()
})

export default router
